#
# @lc app=leetcode.cn id=198 lang=python3
#
# [198] 打家劫舍
#

# @lc code=start
class Solution:
    def rob(self, nums: List[int]) -> int:
        n = len(nums)
        if n==1:
            return nums[0]
        elif n==2:
            return max(nums[0], nums[1])
        state = True
        dp = [0] * n
        dp[0] = nums[0]
        dp[1] = max(nums[0], nums[1])
        state = (dp[1] == nums[1])
        for i in range(2, n, 1):
            if state:
                dp[i] = max(dp[i-1], dp[i-2] + nums[i])
                state = (dp[i] == dp[i-2] + nums[i])
            else:
                dp[i] = dp[i-1] + nums[i]
                state = True
        return dp[n-1]
# @lc code=end

